<?php
	require('group.doc.php');

	//团队课程详情
	$app->get(
		'/group/curriculum/info',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['curriculumid']) && isset($data['memberid'])){
				$place = $db2->select(1, 'tb_group_curriculum', '*,('.$db2->select(2,'tb_group_curriculum_snap','*',array('group_curriculum_id'=>$data['curriculumid'])).')[count]', array('tbid' => $data['curriculumid']));
				$echo['gym_id'] = $place['gym_id'];
				$echo['title'] = $place['title'];
				$echo['img'] = explode($file_subsplit, $place['img'])[0];
				$echo['intro'] = $place['intro'];
				$echo['introimg'] = $place['introimg'];
				$echo['isfree'] = $place['isfree'];
				$echo['people'] = $place['people'];
				$echo['price'] = $place['price'];
				$echo['count'] = $place['count'];
				$echo['coach'] = $place['coach'];
				
				$echo['burningfat'] = $place['burningfat'];
				$echo['peaktime'] = $place['peaktime'];
				$echo['peakofftime'] = $place['peakofftime'];
				
				
				$where = "o.userid=? and o.state=3 and s.group_curriculum_id=?";
				/* if(isset($data['type'])){
				    if($data['type'] == 1)
				        $where .= " and s.peaktime > now()";
				    elseif($data['type'] == 2)
				    $where .= " and s.peaktime < now() and s.peakofftime > now()";
				    elseif($data['type'] == 3)
				    $where .= " and s.peakofftime < now()";
				} */
				 
				$sql = "select count(*) as sum from tb_group_curriculum_snap as s right JOIN tb_order as o on s.idcode=o.idcode where ".$where;
				$ht = $db2->prepare($sql);
				$ht->execute(array($data['memberid'], $data['curriculumid']));
				$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
				$echo['count'] = $tmp[0]['sum'];
				
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	
	
	//团体课程列表
	$app->get(
	    '/group/curriculum/list',
	    function() use($app, $db2, $file_split, $file_subsplit){
	        $data = $app->request()->get();
	        $echo['error'] = "";
	        if($data && isset($data['gymid']) && isset($data['from']) && isset($data['to'])){
	            $where=array('order'=>'tb_group_curriculum.tbid desc', 'gym_id'=>$data['gymid']);
	            
	            if(isset($data['type']))
	               $where['type'] = $data['type'];
	            
	            
	            $dt = date("Y-m-d H:i:s");
	            $where['peakofftime[>]'] = $dt;
	            
	            $echo['count']=$db2->select(2,'tb_group_curriculum','*',$where);
	            $where['limit'] = array($data['from'], $data['to']);
	            
	            if(isset($data['order'])){
	                if($data['order'] == 1){
	                    $where['isfree'] = "1";
	                    //$where['order'] = "isfree desc,price asc";
	                }else if($data['order'] == 2){
	                    $where['isfree'] = "0";
	                    //$where['order'] = "isfree asc,price desc";
	                }
	                
	            }
	            
	            //$db2->set('debug',1);
	            $curriculum=$db2->select(0,'tb_group_curriculum','*,('.$db2->select(4,'tb_group_curriculum_snap','count(*)',array('#tb_group_curriculum_snap.group_curriculum_id'=>'tb_group_curriculum.tbid')).')[count]',$where);
	            foreach($curriculum as $value){
	                $tmp['tbid'] =$value['tbid'];
	                $tmp['gym_id'] =$value['gym_id'];
	                $tmp['title'] =$value['title'];
	                $tmp['img'] = explode($file_subsplit, $value['img'])[0];
	                $tmp['intro'] = $value['intro'];
	                $tmp['introimg'] = $value['introimg'];
	                $tmp['people'] = $value['people'];
	                $tmp['price'] = $value['price'];
	                $tmp['count'] = $value['count'];
	                $tmp['coach'] = $value['coach'];
	                $tmp['burningfat'] = $value['burningfat'];
	                $tmp['isfree'] = $value['isfree'];
	                
	                $tmp['peaktime'] = $value['peaktime'];
	                $tmp['peakofftime'] = $value['peakofftime'];
	                $echo['data'][]=$tmp;
	            }
	        }else{
	            $echo['error'] = "数据缺失，请检查";
	        }
	        echo json_encode($echo);
	    }
	);
	

	//我的团体课程
	$app->get(
		'/group/curriculum/mine',
		function() use($app, $db2, $file_split, $file_subsplit){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['memberid'])){
			    
			    $where = "o.userid=? and o.state=3";
			    if(isset($data['type'])){
			        if($data['type'] == 1)
			            $where .= " and s.peaktime > now()";
			        elseif($data['type'] == 2)
			            $where .= " and s.peaktime < now() and s.peakofftime > now()";
			        elseif($data['type'] == 3)
			            $where .= " and s.peakofftime < now()";
			    }
			    
			    $sql = "select count(*) as sum from tb_group_curriculum where tbid in ".
			    "( select group_curriculum_id from tb_group_curriculum_snap as s left JOIN tb_order as o on s.idcode=o.idcode where ".
			    $where.")";
			    $ht = $db2->prepare($sql);
			    $ht->execute(array($data['memberid']));
			    $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
			    $echo['count'] = $tmp[0]['sum'];
			    
			    $sql = "select * from tb_group_curriculum where tbid in ".
			        "( select group_curriculum_id from tb_group_curriculum_snap as s left JOIN tb_order as o on s.idcode=o.idcode where ".
			        $where.") limit ?,?";
			    $ht = $db2->prepare($sql);
			    $ht->execute(array($data['memberid'], $data['from'], $data['to']));
			    $echo['data'] = $ht->fetchAll(PDO::FETCH_ASSOC);
				
			    foreach($echo['data'] as $key=>&$value){
			        $value['img'] = explode($file_subsplit, $value['img'])[0];
			        
			        $tmp = time();
			        $tmp1 = strtotime($value['peakofftime']);
			        
			        if($tmp>$tmp1){
			            $value['done'] = 1;
			        }else{
			            $value['done'] = 0;
			        }
			    }
			    
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
